﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
namespace ISchool
{
    class RoomOutORIn
    {
        //作者;郭菲
        //描述：地图的放大和缩小及复位功能

        private ESRI.ArcGIS.Controls.AxMapControl mControls { get; set; }
        private InOrOut mDoInOrOut = 0;
        /// <summary>
        /// 放大为1，缩小为2,恢复默认为0
        /// </summary>
        public enum InOrOut
        {
            In = 1,
            Out = 2,
            Pan=3,
            Defualt = 0
        }

        //初始化，锁定操作控件，并根据指令类型分发任务
        public RoomOutORIn(ESRI.ArcGIS.Controls.AxMapControl newControl, InOrOut mInOrOut)
        {
            mControls = newControl;

            //判断控件地图中是否有图层，无图层无法放大缩小
            if (!mZoomIsEmpty())
            {
                mDoInOrOut = mInOrOut;
                if (mDoInOrOut == InOrOut.In)
                {
                    //放大
                    mZoomIn();
                }
                else if (mDoInOrOut == InOrOut.Out)
                {
                    //缩小
                    mZoomOut();
                }
                else if (mDoInOrOut == InOrOut.Pan)
                {
                    //平移
                    IPan();
                }
                else
                {
                    //复位
                    mZoomDefualt();
                }
            }
            else
            {
                return;
            }
        }

        // 检查控件中是否有图层和数据
        private bool mZoomIsEmpty()
        {
            if (mControls != null)
            {
                if (mControls.LayerCount == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //放大
        private void mZoomIn()
        {
            if (!mZoomIsEmpty())
            {
                //获取控件视图对象
                IActiveView mActiveView = mControls.ActiveView;
                //获取当前视图比例
                IEnvelope mEnvelope = mActiveView.Extent;
                //视图比例系数0.6
                mEnvelope.Expand(0.6, 0.6, true);
                //当前视图替换
                mActiveView.Extent = mEnvelope;
                //当前视图刷新
                mActiveView.Refresh();
            }
        }
        //缩小
        private void mZoomOut()
        {
            //同上
            if (!mZoomIsEmpty())
            {
                IActiveView mActiveView = mControls.ActiveView;
                IEnvelope mEnvelope = mActiveView.Extent;
                //比例系数1.5
                mEnvelope.Expand(1.5, 1.5, true);
                mActiveView.Extent = mEnvelope;
                mActiveView.Refresh();
            }
        }
        private void IPan()
        {
            if (!mZoomIsEmpty())
            {
                ICommand pCmd = new ControlsMapPanToolClass();
                pCmd.OnCreate(mControls.Object);
                mControls.CurrentTool = pCmd as ITool;
            }
        }
        //恢复
        private void mZoomDefualt()
        {
            //恢复默认（全屏显示）
            if (!mZoomIsEmpty())
            {
                mControls.Extent = mControls.FullExtent;
                mControls.Refresh();
            }
        }
    }
}
